$(document).ready(function(){
    
    var reg=false;
    
    function validateForm()
    {
        var x=$("#correo").attr('value');
        var atpos=x.indexOf("@");
        var dotpos=x.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
        {
//            alert("Not a valid e-mail address");
            return false;
        }
    
        else
        {
            return true;
        }
    }
    
    $("#correo").keyup(function() {
        var correo = $('#correo').attr('value');
        if(correo==""){
            $('div#checkMail').text("");
            reg=false;
            return false;
        }
        else{
            if(validateForm()==false)
            {
                $('div#checkMail').css("color","orange");
                $('div#checkMail').text("Introduce un formato de correo correcto");
                reg=false;
                return false;
            }
            else
            {
                $.ajax({
                    type: "GET",
                    url: "cgis/comprobarCorreo.cgi", // URL of the Perl script
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    // send username and password as parameters to the Perl script
                    data: "correo=" + correo,
                    // script call was *not* successful
                    error: function(XMLHttpRequest, textStatus, errorThrown) { 
                        $('div#lcheckMail').text("responseText: " + XMLHttpRequest.responseText 
                            + ", textStatus: " + textStatus 
                            + ", errorThrown: " + errorThrown);
                    }, // error 
                    // script call was successful 
                    // data contains the JSON values returned by the Perl script 
                    success: function(data){
                        if (data.error) { // script returned error
                            $('div#checkMail').text("data.error: " + data.error);
                        } // ifd
                        else { // login was successful
                            if(parseInt(data.hits)>0){
                                $('div#checkMail').css("color","red");
                                $('div#checkMail').text("Ese correo ya esta en uso ¿ya eres miembro?");
                                reg=false;

                            }
                            else{
                                $('div#checkMail').css("color","green");
                                $('div#checkMail').text("Está disponible");
                                reg=true;
                            }
                        } //else
                    } // success
                }); // ajax
                return false;
            }
        }
    });


    $("form#loginForm").submit(function() { // loginForm is submitted
        var nombre = $('#nombre').attr('value'); // get username
        var apellidos = $('#apellidos').attr('value'); // get password
        var correo = $('#correo').attr('value'); // get password
        var password = $('#password').attr('value'); // get password
    
        if (nombre && apellidos && correo && password && reg==true) { // values are not empty
            $.ajax({
                type: "GET",
                url: "cgis/agregarUsuario.cgi", // URL of the Perl script
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                // send username and password as parameters to the Perl script
                data: "nombre=" + nombre + "&apellidos=" + apellidos + "&correo=" + correo + "&password=" + password,
                // script call was *not* successful
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    $('div#loginResult').text("responseText: " + XMLHttpRequest.responseText 
                        + ", textStatus: " + textStatus 
                        + ", errorThrown: " + errorThrown);
                    $('div#loginResult').addClass("error");
                }, // error 
                // script call was successful 
                // data contains the JSON values returned by the Perl script 
                success: function(data){
                    if (data.error) { // script returned error
                        $('div#loginResult').text("data.error: " + data.error);
                        $('div#loginResult').addClass("error");
                    } // ifd
                    else { // login was successful
                        $('form#loginForm').hide();
                        $('div#loginResult').text("Te has registrado correctamente, vuelve a la página inicial y logéate");
                        $('div#loginResult').addClass("success");
                    } //else
                } // success
            }); // ajax
        } // if
        else {
            
            $('div#loginResult').text("Entra todos los campos");
            $('div#loginResult').addClass("error");
        } // else
        $('div#loginResult').fadeOut();
        $('div#loginResult').fadeIn();
        return false;
    });


});